/// <reference path="../_References.ts"/>

class Snake
{
    public body;
    public x;
    public y;

    constructor(size)
    {
        // var score = 0; TODO: we never use it xD
        this.x = Math.floor(Game.terrain.width / 2 - 1); // x coordiante calculation for the snake's head
        this.y = Math.floor(Game.terrain.height / 2); // y calcualtion for the snake's head
        this.body = [];

        for (var i = 0; i < size; i++)
            this.body.push(new Point(this.x - i, this.y)); //this is the body creation (in the center of the map)
    }


    public draw = function ()
    {
        var segment;
        for (segment in this.body)
            if (segment === "0")
            {
                Game.drawPoint(this.body[segment], '#8ED6FF');
                //context.stroke();
            }
            else
                Game.drawPoint(this.body[segment], '#218A1D');
    };

    public move = function () //still missing the code for the snake eating the ball working on it by now!
    {
        var point = new Point(this.body[0].x, this.body[0].y);

        switch (Game.direction)
        {
            case Game.DIRECTION.UP: //we can reduce the structure.... by making a function with parameters because the code is almost the same (but i dont' knwo how to make calls oto other methods/functions and go back)
                if (Game.terrain.getAdiacent(this.body[1], Game.DIRECTION.DOWN).equals(this.body[0])) //special case snake is moving down and the guy presses up
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.DOWN);
                else //the snake moves up :)
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.UP);
                break;
            case Game.DIRECTION.DOWN:
                if (Game.terrain.getAdiacent(this.body[1], Game.DIRECTION.UP).equals(this.body[0])) //special case snake is moving up and the guy presses down
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.UP);
                else //the snake moves down :)
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.DOWN);
                break;
            case Game.DIRECTION.RIGHT:
                if (Game.terrain.getAdiacent(this.body[1], Game.DIRECTION.LEFT).equals(this.body[0])) //special case snake is moving left and the guy presses right
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.LEFT);
                else //the snake moves right :)
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.RIGHT);
                break;
            case Game.DIRECTION.LEFT:
                if (Game.terrain.getAdiacent(this.body[1], Game.DIRECTION.RIGHT).equals(this.body[0])) //special case snake is moving right and the guy presses left
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.RIGHT);
                else //the snake moves left :)
                    point = Game.terrain.getAdiacent(point, Game.DIRECTION.LEFT);
        }

        var compux = Math.abs(this.body[0].x - this.body[2].x); //compares the head coordinates with the 3rd body's coordinate
        var compuy = Math.abs(this.body[0].y - this.body[2].y);

        if (compux == 1 &&
            compuy == 1)
            Game.sounds.snakeMoveSound.play();

        this.body.unshift(point);
        if (Game.powerup.Location.x == this.body[0].x &&
            Game.powerup.Location.y == this.body[0].y) //and the ball will be spawned again (and the counter will grow "still missing")
        {
            Game.powerup = new Powerup();
            Game.player.score += Game.powerup.score;
            Game.sounds.snakeEatSound.play();
        }
        else
            this.body.pop();

        if (this.body[0].x == -1 ||
            this.body[0].x == Game.terrain.width ||
            this.body[0].y == -1 ||
            this.body[0].y == Game.terrain.height)
        {
            //      alert('Snake hit the wall! Ouch!'); //needs to be improved
            Game.snake.alive = false;
        }

        for (var i = 4; i < this.body.length; i++)
            if (this.body[0].x == this.body[i].x &&
                this.body[0].y == this.body[i].y) {
                alert('Snake ate itself! Yummy!'); //needs to be improved
                Game.snake.alive = false;
            }
        //break;
        // add the yes no window to restart or do it in the restart function (should be prompted) Hey fool do you want to restart?
        //then goto restart
    };
}